package x.ovo.wechat.bot.http.request.core;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.json.JSONUtil;
import x.ovo.wechat.bot.common.Constant;
import x.ovo.wechat.bot.common.entity.WebSyncResult;
import x.ovo.wechat.bot.common.http.request.ApiRequest;
import x.ovo.wechat.bot.common.http.session.LoginSession;

import java.util.Map;
import java.util.function.Function;

/**
 * Web 同步请求
 *
 * @author ovo, created by 2024/05/03
 */
public class WebSyncRequest extends ApiRequest<WebSyncResult> {

    public WebSyncRequest(LoginSession session) {
        super(session);
    }

    @Override
    public String getMethod() {
        return POST;
    }

    @Override
    public boolean isJson() {
        return true;
    }

    @Override
    public String getUrl() {
        return UrlBuilder.of(this.session.getUrl())
                .addPath(Constant.WEBWXSYNC)
                .addQuery("skey", this.session.getSKey())
                .addQuery("sid", this.session.getWxSid())
                .addQuery("pass_ticket", this.session.getPassTicket())
                .build();
    }

    @Override
    public Map<String, Object> getParameters() {
        super.addParameter("BaseRequest", this.session.getBaseRequest())
                .addParameter("SyncKey", this.session.getSyncKey())
                .addParameter("rr", ~(System.currentTimeMillis() / 1000));
        return super.getParameters();
    }

    @Override
    public Function<String, WebSyncResult> responseHandler() {
        return s -> JSONUtil.toBean(s, WebSyncResult.class );
    }

}
